package com.gjl.lean.leapyear;

/**
 * @author Lemon
 * @date 2020/10/9 09:51
 */
public class LeapYear {

    private static final int[] DAYS_OF_MONTH_CURSOR = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    /**
     * 每个月的天数
     *
     * @param year  年
     * @param month 月
     * @return 月天数
     */
    public static int daysOfMonth(int year, int month) {
        assert year > 0 : "year 必须是大于0的正整数公历年";
        assert month >= 1 && month <= 12 : "month 必须在 [1,12] 之间";
        if (month == 2 && isLeapYear(year)) {
            return 29;
        }
        return DAYS_OF_MONTH_CURSOR[month - 1];
    }

    /**
     * 判断是否闰年
     *
     * @param year 年份
     * @return 闰年 true，非闰年 false
     */
    public static boolean isLeapYear(int year) {
        assert year > 0 : "year 必须是大于0的正整数公历年";
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }

    public static void println(int year, int month) {
        System.out.println("" + year + "," + month + "," + LeapYear.daysOfMonth(year, month));
    }

    public static void main(String[] args) {
        LeapYear.println(2000, 2);
        LeapYear.println(2000, 3);
        LeapYear.println(2000, 4);
        LeapYear.println(2004, 2);
        LeapYear.println(2004, 3);
        LeapYear.println(2004, 4);
        LeapYear.println(2019, 2);
        LeapYear.println(2019, 3);
        LeapYear.println(2019, 4);
    }
}
